package edu.mtu.citizenscience.mushroommapper.activity.main.find;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.openswimsoftware.orm.model.list.ModelArrayList;

import edu.mtu.citizenscience.mushroommapper.MMListActivity;
import edu.mtu.citizenscience.mushroommapper.activity.R;
import edu.mtu.citizenscience.mushroommapper.activity.main.FindActivity;
import edu.mtu.citizenscience.mushroommapper.activity.main.nameselect.NameListAdapter;
import edu.mtu.citizenscience.mushroommapper.model.ModelFamily;
import edu.mtu.citizenscience.mushroommapper.model.family.ModelGenus;
import edu.mtu.citizenscience.mushroommapper.model.family.genus.ModelSpecies;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ListView;
/**
 * @author Corbin Uselton <corbinu@cwuselto@mtu.edu>
 * @copyright (c) 2012 MichiganTech <http://www.mtu.edu>
 * @since 1.0
 * 
 * Name Select Activity Responsible for selecting the mushroom name from a list
 */
public class NameSelectActivity extends MMListActivity {
	// reference var for passing the activity result type
	public static final String RESULT_TYPE = "RESULT_TYPE";
	// reference var for passing the activity result name
	public static final String RESULT_NAME = "RESULT_NAME";
	
	// the model the mushroom family if the family name was passed
	private ModelFamily family;
	// the model the mushroom genus if the genus name was passed
	private ModelGenus genus;
	// the list of possible names to select from
	private List<String> names = new ArrayList<String>();
	// the names list adapter
	private NameListAdapter namesAdapter;
	
	/**
	 * @since 1.0
	 * 
	 * Watches changes in the name input text view
	 */
	private class SearchWatcher implements TextWatcher
	{

		/**
		 * Called after the text view changed
		 * 
		 * @param s - Editable - editable text
		 * @return void
		 */
		public void afterTextChanged(Editable s) {  }

		/**
		 * Called before the text view is changed
		 * 
		 * @param s - CharSequence - sequence of chars that were changed
		 * @param start - int - the start index of the change
		 * @param count - int - the count of chars changed
		 * @param after - int - new text length
		 * @return void
		 */
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {  }

		/**
		 * Called when the text view is changed
		 * 
		 * @param s - CharSequence - sequence of chars that were changed
		 * @param start - int - the start index of the change
		 * @param before - int - old text length
		 * @param count - int - the count of chars changed
		 * @return void
		 */
		public void onTextChanged(CharSequence s, int start, int before, int count) 
		{
			// search the names adapter for the typed sequence
			namesAdapter.search(s + "");
		}
        
    }
	
	/**
	 * Name Select Activity creation handler
	 * 
	 * @param   savedInstanceState - Bundle - Bundle with launch info
	 * @return	void
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		
		this.setContentView(R.layout.selection_activity);
        
		// get the family id from the intent if not found then set to -1
		long familyId = this.getIntent().getExtras().getLong(ModelFamily.FAMILY_ID, -1);
		
		// check if got family id
		if (familyId > -1)
		{
			// get the family model for the passed family id
			this.family = new ModelFamily(this, familyId);
		}
		
		// get the genus id from the intent if not found then set to -1
		long genusId = this.getIntent().getExtras().getLong(ModelGenus.GENUS_ID, -1);
		
		// check if got genus id
		if (genusId > -1)
		{
			// get the genus model for the passed genus id
			this.genus = new ModelGenus(this, genusId);
		}

		// loop over the requested result type
		switch (this.getIntent().getExtras().getInt(RESULT_TYPE))
		{
			case FindActivity.RESULT_TYPE_FAMILY:
				// setup for family result type
				this.setupFamily();
			break;
			case FindActivity.RESULT_TYPE_GENUS:
				// setup for genus result type
				this.setupGenus();
			break;
			case FindActivity.RESULT_TYPE_SPECIES:
				// setup for species result type
				this.setupSpecies();
			break;
			default:
				// no valid result type passed so close the activity
				this.finish();
			break;
		}
		
		// remove the Unknown name from the list
		this.names.remove("Unknown");
		
		// sort the names so they are in alphabetical order
		Collections.sort(this.names);
		
		// readd the the Unknown name at the top of the list
		this.names.add(0, "Unknown");
		
		// create new names list adapter and add the names list
		this.namesAdapter = new NameListAdapter(this, this.names);
		
		// set the list adapter to the names adapter
		this.setListAdapter(this.namesAdapter);
		
		// get the search input edit text and add text changed listener search watcher
		this.aq.id(R.id.searchInput).getEditText().addTextChangedListener(new SearchWatcher());
	}

	/**
	 * onListItemClick hander for when one of the names is clicked
	 * 
	 * @param   listView - ListView - The ListView where the click happened
	 * @param	view - View - The view that was clicked within the ListView
	 * @param	position - int - The position of the view in the list
	 * @param	id - long - The row id of the item that was clicked
	 * @return	void
	 */
	@Override
	public void onListItemClick(ListView listView, View view, int position, long id)
	{
		// default the result to Unknown
		String result = "Unknown";
		
		// get the selected name
		result = this.namesAdapter.getItem(position);
		
		// set the result name as the activty result
		this.setResult(RESULT_OK, new Intent().putExtra(RESULT_NAME, result));
		// finish the name select activity
		this.finish();
	}
	
	/**
	 * Set up the name list for family name selection
	 * 
	 * @param   void
	 * @return	void
	 */
	public void setupFamily()
	{
		// set the title text to family title
		this.aq.id(R.id.title_text).text(R.string.select_title_family);
		
		// find and loop over all family models adding the name to the names list
		for (ModelFamily family: new ModelArrayList<ModelFamily>(this, ModelFamily.class).find())
		{
			this.names.add(family.getName());
		}
	}
	
	/**
	 * Set up the name list for genus name selection
	 * 
	 * @param   void
	 * @return	void
	 */
	public void setupGenus()
	{	
		// set the title text to genus title
		this.aq.id(R.id.title_text).text(R.string.select_title_genus);
		
		// check if have family model
		if (this.family != null) 
		{
			// get and loop over the family genera adding then genus names to names list
			for (ModelGenus genus: this.family.getGenera())
			{
				this.names.add(genus.getName());
			}
			
		} 
		else
		{
			// get and loop over the all genera models adding then genus names to names list
			for (ModelGenus genus: new ModelArrayList<ModelGenus>(this, ModelGenus.class).find())
			{
				this.names.add(genus.getName());
			}
		} 
	}
	
	/**
	 * Set up the name list for species name selection
	 * 
	 * @param   void
	 * @return	void
	 */
	public void setupSpecies()
	{
		// set the title text to species title
		this.aq.id(R.id.title_text).text(R.string.select_title_species);

		// check if have genus model
		if (this.genus != null) 
		{
			// get and loop over the genera species models adding then species names to names list
			for (ModelSpecies species: this.genus.getSpecies())
			{
				this.names.add(species.getName());
			}
		}
	}
	
}
